﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mirkuz.Mapping;

namespace Mirkuz.TrailCompanion
{
    public partial class fAddWaypoint : Form
    {
        public WayPoint P;
        public fAddWaypoint(Mapping.MapPoint _position)
        {
            InitializeComponent();
            if (_position != null)
                P = new WayPoint("", "", _position.Latitude, _position.Longitude, _position.Altitude);
            else
                P = new WayPoint("", "", 0, 0, 0);

            try
            {
                txtAlt.Text = P.Altitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
                txtLong.Text = P.Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
                txtLat.Text = P.Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
            }
            catch { }
        }

        private void mnuCanc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() == "")
                return;

            try
            {
                P.Name = txtName.Text;
                P.Description = txtDesc.Text;
                P.Latitude= double.Parse(txtLat.Text, System.Globalization.CultureInfo.InvariantCulture);
                P.Altitude = (int)double.Parse(txtAlt.Text, System.Globalization.CultureInfo.InvariantCulture);
                P.Longitude= double.Parse(txtLong.Text, System.Globalization.CultureInfo.InvariantCulture);
            }
            catch {
                MessageBox.Show("Invalid data entered","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return; 
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtLat_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                P.Latitude = double.Parse(txtLat.Text, System.Globalization.CultureInfo.InvariantCulture);
            }
            catch {
                e.Cancel = true;
            }
        }

        private void txtLong_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                P.Longitude = double.Parse(txtLong.Text, System.Globalization.CultureInfo.InvariantCulture);
            }
            catch
            {
                e.Cancel = true;
            }
        }

        private void txtAlt_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                P.Altitude = (int)double.Parse(txtAlt.Text, System.Globalization.CultureInfo.InvariantCulture);
            }
            catch
            {
                e.Cancel = true;
            }
        }
    }
}